package com.duoduo.modules.controller;

import com.duoduo.common.util.JWTUtil;
import com.duoduo.modules.service.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 把控制层的公共部分提取出来
 * @author
 * @version 1.0
 * @date 2020/8/5 14:34
 * @description 基础controller
 */
public class BaseController {

    /**
     * 注入全部service
     */
    @Autowired
    AuthUserService userService;


    /**
     * 获取全局属性
     */
//    @Value("${port.url}")
//    protected String portUrl;
//    @Value("${download.file.path}")
//    protected String downloadFilePath;

    /**
     * 创建session、Request、Response等对象
     */
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    /**
     *
     * @Date 2022/11/19  17:29
     * @Version 1.0
     */
    protected String token;


    /**
     * ModelAttribute在每个子类方法调用之前先调用
     * 设置request,response,session这三个对象
     *
     * @param request
     * @param response
     */
    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession(true);
        this.token = request.getHeader("token");
        //可以在此处拿到当前登录的用户
    }

    /**
     * 获取学校的表名
     * @ClassName BaseController
     * @Date 2022/11/23  15:19
     * @Version 1.0
     */
//    public String getStudentTable() {
//        //String schoolId = JWTUtil.resolveClaim(token, "schoolId");
//        String regionId = JWTUtil.resolveClaim(token, "regionId");
//        if (regionId == null){
//            throw new RuntimeException("区域编号为空!");
//        }
//        // 添加表名
//        return "archives_" + regionId;
//    }

}
